﻿using System;
using System.IO;
using Fee;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;

//using Pomelo.EntityFrameworkCore.MySql.Infrastructure;

namespace JwtWebApi.Data
{
    public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .AddJsonFile("appsettings.Development.json")
                .Build();
            var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
            var dbType = configuration["databaeType"];

            if (dbType == "mssql")
            {
                var connectionString = configuration.GetConnectionString("mssql");
                builder.UseSqlServer(connectionString);
            }
            else
            {
                var connectionString = configuration.GetConnectionString("mysql");
               // builder.UseMySQL(connectionString);
                builder.UseMySql(connectionString, // replace with your Connection String
                    mySqlOptions =>
                    {
                        mySqlOptions.ServerVersion(new Version(5, 7, 25),
                            ServerType.MySql); // replace with your Server Version and Type
                    });
            }

            return new ApplicationDbContext(builder.Options);
        }
    }

    public class ApplicationDbContext : FeeDbContext
    {
        public ApplicationDbContext(DbContextOptions options) : base(options)
        {
        }
    }
}